package com.ruoyi.gams.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.gams.vo.DeviceLoginParam;
import com.ruoyi.gams.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gams.mapper.GamsDeviceMapper;
import com.ruoyi.gams.domain.GamsDevice;
import com.ruoyi.gams.service.IGamsDeviceService;

/**
 * 设备Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-03
 */
@Service
public class GamsDeviceServiceImpl implements IGamsDeviceService
{
    @Autowired
    private GamsDeviceMapper gamsDeviceMapper;

    /**
     * 查询设备
     *
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public GamsDevice selectGamsDeviceById(Integer id)
    {
        return gamsDeviceMapper.selectGamsDeviceById(id);
    }

    /**
     * 查询设备列表
     *
     * @param gamsDevice 设备
     * @return 设备
     */
    @Override
    public List<GamsDevice> selectGamsDeviceList(GamsDevice gamsDevice)
    {
        return gamsDeviceMapper.selectGamsDeviceList(gamsDevice);
    }

    /**
     * 新增设备
     *
     * @param gamsDevice 设备
     * @return 结果
     */
    @Override
    public int insertGamsDevice(GamsDevice gamsDevice)
    {
        gamsDevice.setCreateTime(DateUtils.getNowDate());
        return gamsDeviceMapper.insertGamsDevice(gamsDevice);
    }

    /**
     * 修改设备
     *
     * @param gamsDevice 设备
     * @return 结果
     */
    @Override
    public int updateGamsDevice(GamsDevice gamsDevice)
    {
        return gamsDeviceMapper.updateGamsDevice(gamsDevice);
    }

    /**
     * 批量删除设备
     *
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteGamsDeviceByIds(Integer[] ids)
    {
        return gamsDeviceMapper.deleteGamsDeviceByIds(ids);
    }

    /**
     * 删除设备信息
     *
     * @param id 设备主键
     * @return 结果
     */
    @Override
    public int deleteGamsDeviceById(Integer id)
    {
        return gamsDeviceMapper.deleteGamsDeviceById(id);
    }

    @Override
    public Result<GamsDevice> login(DeviceLoginParam param)
    {
        GamsDevice device = gamsDeviceMapper.findFirstByDeviceId(param.getDeviceId());
        if (device == null)
        {
            throw new RuntimeException("设备未找到");
        }
        if (!param.getPassword().equals(device.getPassword()))
        {
            throw new RuntimeException("设备密码不正确");
        }
        device.setIsReadFace(0);
        return Result.success(device);
    }
}
